//
//  FactoryMethodViewController.m
//  DesignPattern
//
//  Created by ClassWizard on 16/3/9.
//  Copyright © 2016年 ClassWizard. All rights reserved.
//

#import "FactoryMethodViewController.h"
#import "Eagle.h"
#import "Swift.h"

@interface FactoryMethodViewController ()

@property (weak, nonatomic) IBOutlet UILabel *showMethodLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *birdSelectionSegmentedControl;

@end

@implementation FactoryMethodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI
- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"工厂模式";
}

#pragma mark - Actions
- (IBAction)clickFlyBtn:(UIButton *)sender {
    switch (self.birdSelectionSegmentedControl.selectedSegmentIndex) {
        case 0:
            [self showFactoryMethodWithBirdType:@"Swift"];
            break;
        case 1:
            [self showFactoryMethodWithBirdType:@"Eagle"];
            break;
        default:
            [self showFactoryMethodWithBirdType:@"Bird"];
            break;
    }
}

#pragma mark - FactoryMethod
//展示工厂模式
- (void)showFactoryMethodWithBirdType:(NSString *)birdClassName {
    Bird *bird = [self birdWithClassName:birdClassName];
    self.showMethodLabel.text = [bird fly];
}

//选择生产什么产品，并生产
- (Bird *)birdWithClassName:(NSString *)className {
    Bird *bird = [[NSClassFromString(className) alloc] init];
    return bird;
}

@end
